
class MyGameAction implements lighttool.ICanvasAction
{
    tex: lighttool.spriteTexture;
    texWhite: lighttool.spriteTexture;
    root: HTMLDivElement;
    controller: Controller;
    palette: Nes.Palette;
    texChr: lighttool.spriteTexture | null = null;
    canvas: lighttool.spriteCanvas;
    constructor(root: HTMLDivElement, canvas: lighttool.spriteCanvas)
    {
        this.canvas = canvas;
        this.root = root;
        {
            let data = new Uint8Array(16);
            for (var i = 0; i < 16; i++)
                data[i] = 255;
            this.texWhite = lighttool.spriteTexture.FromUInt8Array(canvas.webgl, data, 2, 2);
            this.texWhite.SetLinear(false, false);
        }
        this.palette = new Nes.Palette();

        var data = new Uint8Array(512 * 256 * 4);
        for (var y = 0; y < 256; y++)
        {
            for (var x = 0; x < 512; x++)
            {
                var i = y * 512 + x;
                var xb = (x / 8) | 0;
                var yb = (y / 8) | 0;
                data[i * 4 + 0] = 0xff;
                if ((xb + yb) % 2 == 0)
                    data[i * 4 + 1] = 0xff;
                data[i * 4 + 3] = 0xff;
            }
        }
        this.tex = lighttool.spriteTexture.FromUInt8Array(canvas.webgl, data, 512, 256);
        this.tex.SetLinear(false, false);

        this.controller = new Controller(this.root);
        this.loadchr();
    }
    async loadchr(): Promise<void>
    {
        let r = await fetch("data/mario.chr");
        let ab = await r.arrayBuffer();
        var _chr = new Nes.Chr();
        _chr.LoadFromChr(new Uint8Array(ab));
        this.texChr = _chr.ToTexture(this.canvas.webgl, this.palette);
    }
    onresize(c: lighttool.spriteCanvas): void
    {

    }
    ondraw(c: lighttool.spriteCanvas): void
    {
        c.drawTexture(this.tex, new lighttool.spriteRect(0, 0, 320, 240), new lighttool.spriteRect(0, 0, 320 / 512, 240 / 256));
        for (var i = 0; i < 64; i++)
        {
            var x = (i % 16) | 0;
            var y = (i / 16) | 0;
            c.drawTexture(this.texWhite, new lighttool.spriteRect(x * 8, y * 8, 8, 8), new lighttool.spriteRect(0, 0, 1, 1), this.palette.colors[i]);
        }
        if (this.texChr != null)
        {
            c.drawTexture(this.texChr, new lighttool.spriteRect(0, 8 * 8, this.texChr.width, this.texChr.height));
        }
    }
    onpointevent(c: lighttool.spriteCanvas, e: lighttool.canvaspointevent, x: number, y: number): boolean
    {
        return false;
    }
}